$(document).ready(function () {



    layui.use(function () {

        var layer = layui.layer;

        form = layui.form;
        laydate = layui.laydate;

        form.on('checkbox(notice-filter)', function (data) {

            var elem = data.elem; // 获得 checkbox 原始 DOM 对象

            var checked = elem.checked; // 获得 checkbox 选中状态

            if (checked) {
                $('.isNotice').show()
            } else {
                $('.isNotice').hide()
            }

        });


        form.on('submit(search)', function (data) {//监听查询提交

            var field = data.field;
            var rules = [];
            $('.laydate-range-time').each(function () {
                if ($(this).val() != '') {
                    rules.push(
                        {
                            "timeBegin": $(this).val().split(' - ')[0],
                            "timeEnd": $(this).val().split(' - ')[1]
                        }
                    );
                }

            });
            var phoneList = []
            $('input[name="linkPhone"]').each(function () {
                if ($(this).val() != '') {
                    phoneList.push($(this).val());
                }

            });


            new AjaxChain()
                .setUrl('/sign/rule/save')
                .setMethod('post')
                .setData(JSON.stringify({
                    rules: rules
                }))
                .then(data => {

                })
                .catch(err => {
                    console.error('请求异常:', err);
                })
                .send();

            var noticeP = {}

            if ($('input[name="notice"]').prop('checked')) {

                var noticeP = {
                    "notifySms": (field.notifySms == "on") ? 1 : 0,
                    "notifyVms": (field.notifyVms == "on") ? 1 : 0,
                    "linkMan": field.linkMan,
                    "linkPhone": phoneList.join(',')
                }

            } else {

                var noticeP = {
                    "notifySms": 0,
                    "notifyVms": 0,
                    "linkMan": field.linkMan,
                    "linkPhone": phoneList.join(',')
                }
            }


            new AjaxChain()
                .setUrl('/sign/notice/save')
                .setMethod('post')
                .setData(JSON.stringify(
                    noticeP
                ))
                .then(data => {

                    layer.msg('配置成功')

                })
                .catch(err => {
                    console.error('请求异常:', err);
                })
                .send();


            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        initSubMenu();
        queryClockRule();

    });
})
var params = {};//查询条件参数
var form, unit, laydate;
var drawer;//抽屉实例
var curId = null;//当前选中物资ID

// 查询当前打卡规则

function queryClockRule() {

    new AjaxChain()
        .setUrl('/sign/rule/list')
        .then(data => {

            data.forEach(e => {
                addRangeTime(e.timeBegin + ' - ' + e.timeEnd)
            });

        })
        .catch(err => {
            console.error('请求异常:', err);
        })
        .send();


    new AjaxChain()
        .setUrl('/sign/notice/get')
        .then(data => {
            if (data.notifySms == 0 && data.notifyVms == 0) {
                $('.isNotice').hide()
                $('input[name="notice"]').prop('checked', false)

            } else {
                $('.isNotice').show()

                $('input[name="notice"]').prop('checked', true)
            }

            $('input[name="notifySms"]').prop('checked', data.notifySms == 0 ? false : true)
            $('input[name="notifyVms"]').prop('checked', data.notifyVms == 0 ? false : true)

            var phoneList = data.linkPhone.split(',');

            $('input[name="linkMan"]').val(data.linkMan)

            phoneList.forEach((element, index) => {
                addPhone(element, index)
            });

        })
        .catch(err => {
            console.error('请求异常:', err);
        })
        .send();



}


// 点击添加多个时间段

$("#addRangeTime").click(function () {

    addRangeTime()

})

function addRangeTime(time) {
    $('.time-list').append(`
        <div class="layui-inline layui-col-md4 layui-padding-2 laydate-range">
            <input type="text" name="date"  placeholder="时间段${$('.laydate-range-time').length + 1}" value="${time || ''}"  class="layui-input laydate-range-time" lay-options="{}"
               lay-affix="close"
               lay-filter="close" 
               >
        </div>
        `)
    form.render();
    laydate.render({
        elem: '.laydate-range-time',
        type: 'time',
        range: true,
        format: 'HH:mm:ss'

    });


    form.on('input-affix(close)', function (data) {

        var elem = data.elem;
        $(elem).parent().parent().remove()


    });

}

// 点击添加多个手机号

$("#addPhone").click(function () {
    addPhone()
})

function addPhone(val, index) {


    if (index == 0) {

        $('input[ name="linkPhone"]').val(val)

    } else {
        if ($('.phone-list-input').length == 2) {
            $('#addPhone').hide()
        }


        $('.phone-list').append(`
        <div class="layui-form-item layui-inline layui-col-md12 layui-padding-2">
            <input type="text" value="${val || ''}" name="linkPhone"  placeholder="手机号${$('.phone-list-input').length + 1}"
                class="layui-input phone-list-input">
            <img onclick=removePhone(this) class="phone-icon" src="../static/img/delete.png">
        </div>
        `)
    }


}

function removePhone(t) {

    $(t).parent().remove()

    if ($('.phone-list-input').length < 3) {
        $('#addPhone').show()
    }
}